﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DiaryPlanner
{
    public partial class PlannerWeek : UserControl
    {
        Planner planner;

        List<Label> weekList = new List<Label>();
        List<Label> timeList = new List<Label>();

        const int week = 7;
        const int time = 24;
        const int half = 2;
        const int labelNum = week * time * half;
        const int labelTimeNum = time * half;

        public PlannerWeek()
        {
            InitializeComponent();
            initWeek();
        }

        private void initWeek()
        {
            int i;

            for (i = 0; i < labelNum; i++)
                weekList.Add(new System.Windows.Forms.Label());

            for (i = 0; i < labelTimeNum; i++)
                timeList.Add(new System.Windows.Forms.Label());

            this.SuspendLayout();

            for (i = 0; i < labelNum; i++)
            {
                weekList[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                weekList[i].Location = new System.Drawing.Point(0, 0);
                weekList[i].Margin = new System.Windows.Forms.Padding(0);
                weekList[i].Name = "labelW"+i.ToString();
                weekList[i].Size = new System.Drawing.Size(54, 25);
                weekList[i].Text = "";
            }

            for (i = 0; i < labelTimeNum; i++)
            {
                timeList[i].Location = new System.Drawing.Point(0, 0);
                timeList[i].Margin = new System.Windows.Forms.Padding(0);
                timeList[i].Name = "labelT" + i.ToString();
                timeList[i].Size = new System.Drawing.Size(53, 25);
                if (i % 2 == 0) timeList[i].Text = getTime(i/2);
                else timeList[i].Text = "";
            }

            for (i = 0; i < labelNum; i++)
            {
                if (i % 7 == 0) this.flowLayoutPanel1.Controls.Add(timeList[i/7]);
                this.flowLayoutPanel1.Controls.Add(weekList[i]);
            }

            this.ResumeLayout(false);
            this.PerformLayout();

            //클릭 이벤트 핸들러 등록
            for (i = 0; i < weekList.Count; i++)
                weekList[i].Click += new System.EventHandler(this.weekList_Click);

            //더블 클릭 이벤트 핸들러 등록
            for (i = 0; i < weekList.Count; i++)
                weekList[i].DoubleClick += new System.EventHandler(this.weekList_DoubleClick);
        }

        private string getTime(int time)
        {
            string temp = "";

            if (time < 12)
            {
                temp = "오전" + (12 - time).ToString()+"시";
            }
            else if (time == 12)
                temp = "오후12시";
            else
            {
                temp = "오후" + (time - 12).ToString()+"시";
            }

            return temp;
        }

        private void weekList_Click(object sender, EventArgs e)
        {
            Label label = (Label)sender;

            for (int i = 1; i < weekList.Count; i++)
                weekList[i].BackColor = System.Drawing.Color.White;

            label.BackColor = System.Drawing.Color.Blue;
        }

        private void weekList_DoubleClick(object sender, EventArgs e)
        {
            PlannerEdit PlannerEditObject = new PlannerEdit();
            PlannerEditObject.initPlannerEdit(planner, this);
            PlannerEditObject.ShowDialog();
        }

        public void savePlanner(Planner planner)
        {
            this.planner = planner;
        }

    }
}

